package com.ruoyi.system.service.sso;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jayway.jsonpath.JsonPath;
import com.ruoyi.system.domain.IndsvrSsoApp;
import com.ruoyi.system.domain.IndsvrSsoAppUserMap;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import okhttp3.Cookie;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.util.HashMap;

@Component
public class OneKvm  implements SimulationLogin{
    @Override
    public Object run(IndsvrSsoApp app, IndsvrSsoAppUserMap user, HttpServletRequest request) throws Exception {
        return run(user.getUsername(),user.getPassword(),app.getBaseUrl());
    }

    public Auths run(String user, String pwd, String baseUrl) throws Exception{


        OkHttpUtils okHttpUtils = new OkHttpUtils();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("user",user);
        stringStringHashMap.put("passwd",pwd);
        String url = baseUrl+"/api/auth/login";
        String s = okHttpUtils.postForForm(url,stringStringHashMap);

        HashMap<String, Cookie> cookie = okHttpUtils.getCookie(new URI(url).getHost());

        String authToken = cookie.get("auth_token").value();


        Auths auths = new Auths();
        auths.token= authToken;
        return auths;
    }


    @Data
    public static class Auths{
        String token;
    }

    @Data
    public static class Token{
        private Object data;
        private String expire = null;

    }
}
